__version__ = "1.1.0"
import os

import fnmatch

#获取文件总数
#folder_path 文件夹路径
#返回文件总数
def get_dir_col (folder_path):
   file = os.listdir(folder_path)
   numfile = len(file)
   return numfile

#2024_9_6 shp 判断文件是否存在，如果存在就加1
#file_name 文件名
#返回文件名
def file_check(file_name):
    temp_file_name = file_name
    i = 1
    while i:
       #rint(temp_file_name)
        #print(os.path.exists("./" + temp_file_name))
        if os.path.exists("./" + temp_file_name):
            #name文件名,suffix文件名后缀
            name, suffix = file_name.split('.')
            name += '(' + str(i) + ')'
            temp_file_name = name+'.'+suffix
            i = i+1
        else:
            return temp_file_name


#模糊查找文件夹里的文件
#folder_path 文件路径
#query 关键词
#返回文件名
def fuzzy_search(folder_path, query):
   # 存储匹配的文件
   matched_files = []
   # 验证文件夹路径
   if not os.path.isdir(folder_path):
      print(f"路径 {folder_path} 不是一个有效的文件夹！")
      return matched_files
   # 遍历文件夹
   for root, dirs, files in os.walk(folder_path):
      for filename in fnmatch.filter(files, f"*{query}*"):
         matched_files.append(filename)
         return matched_files

#获取所有文件夹里的子文件名
#bcp_path bcp文件夹路径
#bcp_suffix后缀名
#返回bcp文件目录
def get_dir_bcp_name(bcp_path,bcp_suffix):
   #存储bcp返回目录
   matched_bcps = []
   count = 0
   files = os.listdir(bcp_path)
   files_count = len(files)
   for file in files:
        count+=1
        file_path = os.path.join(bcp_path, file)
        if os.path.isdir(file_path):
            print("共"+ str(files_count) + ",第"+ str(count) +",正在查询"+ file_path)
            bcp_name = fuzzy_search(os.path.join(bcp_path,file),bcp_suffix )
            #print(bcp_name)
            if(bcp_name != None):
               matched_bcps.append(os.path.join(file_path,bcp_name[0]))
   return matched_bcps
   #for file in files:
   #   files = fuzzy_search(os.path.join(bcp_path,file),bcp_suffix )
   #   #print(files[0])
   #   matched_bcps.append(os.path.join(bcp_path,file))
   #return matched_bcps

def get_dir_bcp_name(bcp_path,bcp_suffix,bcps_find):
 #存储bcp返回目录
   matched_bcps = []
   count = 0
   files = os.listdir(bcp_path)
   files_count = len(bcps_find)
   for bcp_find in bcps_find:
      for file in files:
         if bcp_find == file:
            count+=1
            file_path = os.path.join(bcp_path, file)
            if os.path.isdir(file_path):
                  print("共"+ str(files_count) + ",第"+ str(count) +",正在查询"+ file_path)
                  bcp_name = fuzzy_search(os.path.join(bcp_path,file),bcp_suffix )
                  #print(bcp_name)
                  if(bcp_name != None):
                     matched_bcps.append(os.path.join(file_path,bcp_name[0]))
         
         
   return matched_bcps

if __name__ == '__main__':

   dir = 'Z:\\bcp'
   #dir = 'D:\dhz\wokring\cs_bcp'
   #progress_bar()
   #get_dir_bcp_name(dir,'070400')
   ans = get_dir_bcp_name(dir,'070400')
   print (ans)
